package com.example.administrator.im.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.example.administrator.im.utils.EMUtils;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;

/**
 * create:2018/9/25 21:27
 * author: 姚鸿杰
 **/
public class MsgListenerService extends Service {
    public MsgListenerService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private final String TAG = "MsgService";
    private EMMessageListener mMsgListener;
    private EMContactListener mMContactListener;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate");
        if (mMsgListener == null) {
            mMsgListener = EMUtils.getMsgListener();
        }
        if (mMContactListener == null) {
            mMContactListener = EMUtils.getContactListener();
        }
        EMClient.getInstance().chatManager().addMessageListener(mMsgListener);
        EMClient.getInstance().contactManager().setContactListener(mMContactListener);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
        EMClient.getInstance().chatManager().removeMessageListener(mMsgListener);
        Intent localIntent = new Intent(this, MsgListenerService.class);
//        startService(localIntent);
    }

}
